package com.erp.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.order.api.domain.ErpSellerOrderExtend;
import com.erp.order.mapper.ErpSellerOrderExtendMapper;
import com.erp.order.service.IErpSellerOrderExtendService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单延伸Service业务层处理
 *
 * @author erp
 * @date 2023-07-11
 */
@RequiredArgsConstructor
@Service
public class ErpSellerOrderExtendServiceImpl implements IErpSellerOrderExtendService {

    private final ErpSellerOrderExtendMapper baseMapper;



    /**
     * 根据订单号，获取订单延伸表信息
     * @param orderId
     * @return
     */
    @Override
    public ErpSellerOrderExtend queryByOrderId(String orderId){
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerOrderExtend>()
                .eq(ErpSellerOrderExtend::getOrderId, orderId));

    }

    @Override
    public R<Object> queryOrderSendGoodsInfo(Long orderId) {
        ErpSellerOrderExtend orderExtend = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerOrderExtend>().eq(ErpSellerOrderExtend::getOrderId, orderId)
        );
        if (orderExtend == null) {
            return R.fail("当前订单暂无声明发货信息");
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("logisticsService", orderExtend.getLogisticsService());
            map.put("orderId", orderExtend.getOrderId());
            map.put("otherLogisticsPrice", orderExtend.getOtherLogisticsPrice());
            map.put("realityLogisticsPrice", orderExtend.getRealityLogisticsPrice());
            map.put("logisticsProof", orderExtend.getLogisticsProof());
            map.put("isCainiao", orderExtend.getIsCainiao());
            map.put("intlTrackingNo", orderExtend.getIntlTrackingNo());
            return R.ok(map);
        }
    }

    @Override
    public R<Object> saveShipmentInfoForOther(ErpSellerOrderExtend erpSellerOrderExtend) {
        Long orderId = erpSellerOrderExtend.getOrderId();
        if (orderId == null) {
            return R.fail("订单id为空");
        }
        ErpSellerOrderExtend orderExtend = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerOrderExtend>().eq(ErpSellerOrderExtend::getOrderId, orderId)
        );
        if (orderExtend == null) {
            orderExtend = new ErpSellerOrderExtend();
            orderExtend.setOrderId(orderId);
        }
        orderExtend.setIntlTrackingNo(erpSellerOrderExtend.getIntlTrackingNo());
        orderExtend.setOtherLogisticsPrice(erpSellerOrderExtend.getOtherLogisticsPrice());
        orderExtend.setRealityLogisticsPrice(erpSellerOrderExtend.getRealityLogisticsPrice());
        orderExtend.setLogisticsService("other");
        orderExtend.setLogisticsProof(erpSellerOrderExtend.getLogisticsProof());
        orderExtend.setRevision("saveShipmentInfoForOther");
        baseMapper.insertOrUpdate(orderExtend);
        //将运费保存至统计表中
//        ErpOrderInfoStatistics orderInfoStatistics = orderInfoStatisticsMapper.selectOne(new LambdaQueryWrapper<ErpOrderInfoStatistics>().eq(ErpOrderInfoStatistics::getOrderId, orderId));
//        if (orderInfoStatistics != null) {
//            String freight = erpSellerOrderExtend.getRealityLogisticsPrice();
//            String otherExpenses = erpSellerOrderExtend.getOtherLogisticsPrice();
//            // 总费用
//            BigDecimal totalFreight = new BigDecimal(freight).add(new BigDecimal(otherExpenses));
//            orderInfoStatistics.setLogisticsPrice(totalFreight.toString());
//            orderInfoStatisticsMapper.updateById(orderInfoStatistics);
//        }
        return R.ok("已保存信息");
    }

//    /**
//     * 查询订单延伸
//     */
//    @Override
//    public ErpSellerOrderExtendVo queryById(Long id){
//        return baseMapper.selectVoById(id);
//    }
//
//    /**
//     * 查询订单延伸列表
//     */
//    @Override
//    public TableDataInfo<ErpSellerOrderExtendVo> queryPageList(ErpSellerOrderExtendBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ErpSellerOrderExtend> lqw = buildQueryWrapper(bo);
//        Page<ErpSellerOrderExtendVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
//    }
//
//    /**
//     * 查询订单延伸列表
//     */
//    @Override
//    public List<ErpSellerOrderExtendVo> queryList(ErpSellerOrderExtendBo bo) {
//        LambdaQueryWrapper<ErpSellerOrderExtend> lqw = buildQueryWrapper(bo);
//        return baseMapper.selectVoList(lqw);
//    }
//
//    private LambdaQueryWrapper<ErpSellerOrderExtend> buildQueryWrapper(ErpSellerOrderExtendBo bo) {
//        Map<String, Object> params = bo.getParams();
//        LambdaQueryWrapper<ErpSellerOrderExtend> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getOrderId() != null, ErpSellerOrderExtend::getOrderId, bo.getOrderId());
//        lqw.eq(StringUtils.isNotBlank(bo.getRealityLogisticsPrice()), ErpSellerOrderExtend::getRealityLogisticsPrice, bo.getRealityLogisticsPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getOtherLogisticsPrice()), ErpSellerOrderExtend::getOtherLogisticsPrice, bo.getOtherLogisticsPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getIntlTrackingNo()), ErpSellerOrderExtend::getIntlTrackingNo, bo.getIntlTrackingNo());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsProof()), ErpSellerOrderExtend::getLogisticsProof, bo.getLogisticsProof());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsType()), ErpSellerOrderExtend::getLogisticsType, bo.getLogisticsType());
//        lqw.eq(StringUtils.isNotBlank(bo.getReturnFreight()), ErpSellerOrderExtend::getReturnFreight, bo.getReturnFreight());
//        lqw.eq(StringUtils.isNotBlank(bo.getReturnFreightStatus()), ErpSellerOrderExtend::getReturnFreightStatus, bo.getReturnFreightStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerOrderExtend::getRevision, bo.getRevision());
//        return lqw;
//    }
//
//    /**
//     * 新增订单延伸
//     */
//    @Override
//    public Boolean insertByBo(ErpSellerOrderExtendBo bo) {
//        ErpSellerOrderExtend add = BeanUtil.toBean(bo, ErpSellerOrderExtend.class);
//        validEntityBeforeSave(add);
//        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setId(add.getId());
//        }
//        return flag;
//    }
//
//    /**
//     * 修改订单延伸
//     */
//    @Override
//    public Boolean updateByBo(ErpSellerOrderExtendBo bo) {
//        ErpSellerOrderExtend update = BeanUtil.toBean(bo, ErpSellerOrderExtend.class);
//        validEntityBeforeSave(update);
//        return baseMapper.updateById(update) > 0;
//    }
//
//    /**
//     * 保存前的数据校验
//     */
//    private void validEntityBeforeSave(ErpSellerOrderExtend entity){
//        //TODO 做一些数据校验,如唯一约束
//    }
//
//    /**
//     * 批量删除订单延伸
//     */
//    @Override
//    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
//        if(isValid){
//            //TODO 做一些业务上的校验,判断是否需要校验
//        }
//        return baseMapper.deleteBatchIds(ids) > 0;
//    }

}
